import axios, { Axios, AxiosRequestConfig } from "axios";

// 这个类没有实际作用，唯一的作用是消除get返回的类型错误警告
class JSONAxios extends Axios {
    get<T = any, R = T, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R> {
        
        return super.get<T, R, D>(url, config);
    };
}
export const jsonRequest = axios.create({
    timeout: 5000,
    headers: {
        'Content-Type': 'application/json'
    }
}) as JSONAxios;
jsonRequest.interceptors.response.use(response => {
    const code = response.status;
    if ((code >= 200 && code < 300) || code === 304) {

        return Promise.resolve(response.data);
    } else {
        console.error("Server cannot deal with request, the error message:", response.status)
        // 响应错误逻辑处理 5xx 4xx 等等
        // this.emit("http-request-fail");
        return Promise.reject(response);
    }
})